package com.gopay.webtest.controller.cbrmb;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans7024Handler;
import com.gopay.webtest.vo.RemitInfo;
import com.gopay.webtest.xml.XmlDao;

@Controller
@RequestMapping("/cbrmb")
public class CbRmbRemitController extends BaseController{

    private static Logger logger = Logger.getLogger(CbRmbReturnController.class);

    @Resource(name = "xmlDao")
    private XmlDao xmlDao = null;
    @Resource(name = "trans7024Handler")
    private Trans7024Handler trans7024Handler;

    @RequestMapping(method = RequestMethod.GET, value = "/remit")
    public ModelAndView remitCbrmb() {

        ModelAndView mv = new ModelAndView("gopay.webtest.pay.remit_cbrmb");

        // 加载xml数据到
        try {
            Map<String, String> map = this.xmlDao.readData4Map("cbrmbremit", "cbrmbremitinfo");
            mv.addObject("cbrmbremitinfo", map);
            mv.setViewName("gopay.webtest.pay.remit_cbrmb");
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mv;

    }

    @RequestMapping("/remit/submit")
    public ModelAndView addRemitCbrmb(RemitInfo remitInfo) {

        ModelAndView mv = new ModelAndView("gopay.webtest.pay.remit_cbrmb_submit");

        /*** 对表单数据进行处理 begin */

        try {
            // 进行加签
            trans7024Handler.sign(remitInfo.getTranCode()+"_" + remitInfo.getVersion(), "signType", "signValue", remitInfo,getRequest());
        } catch (Exception e) {

            logger.error("加签出错");
            e.printStackTrace();
        }
        if (!remitInfo.getMerchantEncode().equals("2")) {
        	 mv.setViewName("gopay.webtest.pay.remit_cbrmb_submit_gbk");
        } else {
        	 mv.setViewName("gopay.webtest.pay.remit_cbrmb_submit");
        }
        mv.addObject("remitInfo", remitInfo);
        return mv;
    }

}
